package de.fhaugsburg.games.boardgames.scrabble.scrabbleboard;

import de.fhaugsburg.games.boardgames.*;

public class ScrabbleBoard extends Board <LetterPiece, ScrabbleSquareDetails> {

	
	public final static int BOARD_WIDTH = 15;
	public final static int BOARD_HEIGHT = 15;
	
	
	/**
	 * Constructor
	 */
	public ScrabbleBoard() {
		super(BOARD_WIDTH, BOARD_HEIGHT);
		layoutScrabbleBoard();
	}
	
	
	/**
	 * layout the special detail-squares on the board (letter/word multipliers)
	 */
	public void layoutScrabbleBoard(){
 
		/*
		 * Char Array for one quarter
		 * Don't do this at home kidz!
		 * a = simple
		 * b = DoubleLetter
		 * c = TrippleLetter
		 * d = DoubleWord
		 * e = TripleWord
		 */
		
		// a = row 1, b = row 2 ...
		char[] a = {'e','a','a','b','a','a','a','e'};
		char[] b = {'a','d','a','a','a','c','a','a'};
		char[] c = {'a','a','d','a','a','a','b','a'};
		char[] d = {'b','a','a','d','a','a','a','b'};
		char[] e = {'a','a','a','a','d','a','a','a'};
		char[] f = {'a','c','a','a','a','c','a','a'};
		char[] g = {'a','a','b','a','a','a','b','a'};
		char[] h = {'e','a','a','b','a','a','a','a'};		//center square has no multiplier?
		char quarterArray[][] = {a,b,c,d,e,f,g,h};

		// now set details
		boolean arr_i = false;
		boolean arr_j = false;
		int row = 0;
		int col = 0;
		int i = 0;
		int j = 0;
		
		ScrabbleSquareDetails squareMultiplier;
		
		
		while (row < getHeight()  ) {
			
			while (col < getWidth() ) {
				
//				System.out.println("init " + row + col);
				
				// check wich type
				if (quarterArray[i][j] == 'a') {
					// SIMPLE Type
					squareMultiplier = new ScrabbleSquareDetails(SquareMultiplier.SIMPLE);
					setDetails(squareMultiplier, row, col);
				}
				
				else if (quarterArray[i][j] == 'b') {
					// DoubleLetter
					squareMultiplier = new ScrabbleSquareDetails(SquareMultiplier.LETTER_DOUBLE);
					setDetails(squareMultiplier, row, col);
				}
				
				else if (quarterArray[i][j] == 'c') {
					// DoubleLetter
					squareMultiplier = new ScrabbleSquareDetails(SquareMultiplier.LETTER_TRIPLE);
					setDetails(squareMultiplier, row, col);
				}
				
				else if (quarterArray[i][j] == 'd') {
					// DoubleLetter
					squareMultiplier = new ScrabbleSquareDetails(SquareMultiplier.WORD_DOUBLE);
					setDetails(squareMultiplier, row, col);
				}
				
				else if (quarterArray[i][j] == 'e') {
					// DoubleLetter
					squareMultiplier = new ScrabbleSquareDetails(SquareMultiplier.WORD_TRIPLE);
					setDetails(squareMultiplier, row, col);
				}

				if (j == 7) {arr_j = true;};
				if (arr_j) {j--;}
				else {j++;}
				col++;
			}
			
			col = 0;
			j = 0;
			arr_j = false;
			
			if (i == 7) {arr_i = true;};
			if (arr_i) {i--;}
			else {i++;}
			row++;
		}

	}
	
	
	
	
	/**
	 * check, if entire board is empty
	 * @return
	 */
	public boolean isEmpty(){
		for (int i = 0; i < getHeight(); i++) {
			for (int j = 0; j < getWidth(); j++) {
				if (this.getDetails(i, j).getPieceFixed() != null) return false;
			}
		}
		return true;
	}
	
	
	
	
	
	/**
	 * returns the board as a String
	 */
	public String toString() {
		
		StringBuilder rs = new StringBuilder();
		
		// first row is Column label
		for (int i = 0; i < getWidth(); i++) {
			if (i < 10) {rs.append(" " +(i+1)+ " ");}
			else {rs.append(" "+(i+1));}
		}
		rs.append("\n");
		
			for (int i = 0; i < getWidth(); i++) {
				// print Row label
				rs.append(Character.toString((char) ('A'+i)));
			
					for (int j = 0; j < getHeight(); j++) {
						// print real board row
						if (this.getPiece(i,j) == null) {
							//print detail
							if (i == 7 && j == 7){
								rs.append(" * ");			//marks center of the board
							}else{
								rs.append(this.getDetails(i, j).getSymbol());
							}
						} else {
							//print piece
							String theSymbol = this.getPiece(i,j).getSymbol();
							if (this.getDetails(i, j).getPieceFixed() == null){
								//piece is temporary
								rs.append(theSymbol.toLowerCase());
							}else{
								//fixed piece
								rs.append(theSymbol.toUpperCase());
							}
						}
					}
			
				// print Row label
				rs.append(Character.toString((char) ('A'+i)));
				rs.append("\n");
			}
			
		// last row is also Column label
		for (int i = 0; i < getWidth(); i++) {
			if (i < 10) {rs.append(" " +(i+1)+ " ");}
			else {rs.append(" "+(i+1));}
		}
		
		rs.append("\n");
	return rs.toString();
	}
	
	
	
}
